//
//  UploadItem.m
//  iFlickr
//
//  Created by 张 霄鹤 on 09-5-17.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//


#import "UploadItem.h"
#import "UIImage+Extras.h"


@implementation UploadItem

@synthesize title;
@synthesize description;
@synthesize isPublic;
@synthesize isFriend;
@synthesize isFamily;
@synthesize tags;
@synthesize uploadState;
@synthesize image;
@synthesize photoid;

-(id)init{
	if(self = [super init]){
		self.title = @"Image n";
		self.description = @"An Image";
		self.tags = @"tags";
		self.isPublic = YES;
		self.isFriend = NO;
		self.isFamily = NO;
		self.uploadState = US_NEEDUPLOAD;
	}
	return self;
}

/**
 * 反序列化
 */
- (id)initWithCoder:(NSCoder *)decoder{
	self = [super init];
	title = [[decoder decodeObjectForKey:@"ITITLE"] retain];
	description = [[decoder decodeObjectForKey:@"IDESC"] retain];
	tags = [[decoder decodeObjectForKey:@"ITAGS"] retain];
	NSData *idata = [[decoder decodeObjectForKey:@"IIMG"] retain];
	image = [[UIImage alloc] initWithData:idata];
	NSString *tmp = nil;
	tmp = [decoder decodeObjectForKey:@"IPUB"];
	if(tmp){
		isPublic = YES;
		isFriend = NO;
		isFamily = NO;
	}else{
		tmp = [decoder decodeObjectForKey:@"IFRI"];
		if(tmp){
			isFriend = [tmp isEqualToString:@"true"];
		}
		tmp = [decoder decodeObjectForKey:@"IFAM"];
		if(tmp){
			isFamily = [tmp isEqualToString:@"true"];
		}
	}
	uploadState = US_NEEDUPLOAD;
	return self;
}

/**
 * 序列化
 */
- (void)encodeWithCoder:(NSCoder *)encoder{
	[encoder encodeObject:title forKey:@"ITITLE"];
	[encoder encodeObject:description forKey:@"IDESC"];
	[encoder encodeObject:tags forKey:@"ITAGS"];
	// NSData *data = [self getImageData];
	if(data == nil){
		[self getImageData:0];
	}
	[encoder encodeObject:data forKey:@"IIMG"];
	if(isPublic){
		[encoder encodeObject:@"true" forKey:@"IPUB"];		
	}else {
		NSString *tmp = nil;
		tmp = (isFriend?@"true":@"false");
		[encoder encodeObject:tmp forKey:@"IFRI"];
		tmp = (isFamily?@"true":@"false");
		[encoder encodeObject:tmp forKey:@"IFAM"];
	}	
}


/**
 * 获取图像的原始数据，目前使用 PNG 格式
 */
-(NSData *)getImageData:(NSInteger)type{
	if(self.image){
			if (type == 0) {
				data = [UIImagePNGRepresentation(self.image) retain];
			}else if(type == 1){
				// TODO smaller data
				CGFloat iw = self.image.size.width;
				CGFloat ih = self.image.size.height;
				CGFloat cw,ch;
				CGFloat cc = 320;
				CGFloat sc;
				UIImage *uimg = nil;
				if (iw > cc || ih > cc) {
					if (iw > ih) {
						sc = cc / iw;
					}else{
						sc = cc / ih;
					}
					cw = sc * iw;
					ch = sc * ih;
					uimg= [self.image imageByScalingProportionallyToSize:CGSizeMake(cw, ch)];
				}else {
					uimg = self.image;
				}
				data = [UIImagePNGRepresentation(uimg) retain];
			}else {
				// TODO Support another type
			}
		return data;
	}
	return nil;
}

-(NSDictionary *)getItemInfo{
	NSMutableDictionary *ret = [[[NSMutableDictionary alloc] init]  autorelease];
	
	[ret setObject:title forKey:@"title"];
	
	if(description != nil){
		[ret setObject:description forKey:@"description"];
	}
	
	if(tags != nil){
		[ret setObject:tags forKey:@"tags"];
	}
	
	// 如果是公开的，那么家人，朋友同样默认是公开的
	if(isPublic){
		[ret setObject:@"1" forKey:@"is_public"];
	}else {
		[ret setObject:@"0" forKey:@"is_public"];
		
		if(isFriend){
			[ret setObject:@"1" forKey:@"is_friend"];
		}else {
			[ret setObject:@"0" forKey:@"is_friend"];
		}
		
		if(isFamily){
			[ret setObject:@"1" forKey:@"is_family"];
		}else {
			[ret setObject:@"0" forKey:@"is_family"];
		}	
	}
	
	return ret;
}

/**
 * 有多少项可以修改，用于 UITableView中显示 Cell个数
 */
-(NSInteger)numberOfCanModify{
	return 6;
}

/**
 * 获取相应项目的标题，名称
 */
-(NSString *)getCellTitle:(NSInteger)index{
	NSString *ret = nil;
	switch (index) {
		case 0:
			ret = @"title";
			break;
		case 1:
			ret = @"description";
			break;
		case 2:
			ret = @"tags";
			break;
		case 3:
			ret = @"isPublic";
			break;
		case 4:
			ret = @"isFriend";
			break;
		case 5:
			ret = @"isFamily";
			break;
		default:
			break;
	}
	return ret;
}

- (void)dealloc{
	if(title){
		[title release];
	}
	if(description){
		[description release];
	}
	if(tags){
		[tags release];
	}
	if(image){
		[image release];
	}
	if(photoid){
		[photoid release];
	}
	[super dealloc];
}

@end
